﻿using System.Collections.Generic;
using System.Linq;
using System.Xml;
using MediaMover.Common.TV;

namespace MediaMover.TvDb
{
    public interface IAccess
    {
        IEnumerable<Show> LookupSeriesByName(string seriesName);
        Show LookupSeriesById(Show show);
    }

    public class Access : IAccess
    {
        //http://thetvdb.com/api/340B31581F7A9932/series/95491/all/

        private const string ApiKey = "340B31581F7A9932";
        private const string TvDbUriBase = "http://thetvdb.com";
        private const string SeriesLocationFormat = "{0}/api/GetSeries.php?seriesname={1}&language=en";
        private const string SeriesInformationFormat = "{0}/api/340B31581F7A9932/series/{1}/all";

        public IEnumerable<Show> LookupSeriesByName(string seriesName)
        {
            XmlDocument document = new XmlDocument();
            document.Load(string.Format(SeriesLocationFormat, TvDbUriBase, seriesName));
            IList<Show> seriesCollection = new List<Show>();
            XmlNodeList nodes = document.GetElementsByTagName("Series");
            foreach (XmlNode node in nodes)
            {
                Show series = new Show
                {
                    Code = int.Parse(node["seriesid"].InnerText),
                    Title = node["SeriesName"].InnerText ?? "N/A",
                    Description = GetProperty(node, "Overview")
                };
                seriesCollection.Add(series);
            }
            return seriesCollection;
        }

        public Show LookupSeriesById(Show show)
        {
            XmlDocument document = new XmlDocument();
            document.Load(string.Format(SeriesInformationFormat,TvDbUriBase,show.Code));
            var nodes = document.GetElementsByTagName("Episode");
            foreach (XmlNode node in nodes)
            {
                Episode episode = new Episode();
                episode.Title = node["EpisodeName"].InnerText ?? "N/A";
                episode.Description = node["Overview"].InnerText;
                episode.Number = int.Parse(node["EpisodeNumber"].InnerText);
                int seasonNumber = int.Parse(node["SeasonNumber"].InnerText);
                if (show.Seasons.Where(x=>x.Number == seasonNumber).Count() == 0)
                {
                    var season = new Season() {Number = seasonNumber};
                    season.Episodes.Add(episode);
                    show.Seasons.Add(season);
                }
                else
                {
                    show.Seasons.First(x=>x.Number == seasonNumber).Episodes.Add(episode);
                }
            }
            return show;
        }

        private static string GetProperty(XmlNode node, string property)
        {
            try
            {
                return node[property].InnerText;
            }
            catch
            {
                return "N/A";
            }
        }
    }
}
